<!--用车申请页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆维修</span>
    <!-- :disabled="user.parentId==null" 如果当前登录人没有上级,禁用申请用车按钮 总裁用车不用申请 -->
    <el-button type="primary" style="float:right;margin-top:13px;"
    @click="addApplicationDialogVisible=true" :disabled="user.parentId==null">录入维修车辆</el-button>
  </div>
  <!-- 申请用车弹窗 -->
  <el-dialog title="创建申请单" v-model="addApplicationDialogVisible" style="width:1000px;padding:40px;">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="送修人">
            <!-- 申请人是当前登录人,需要从localStorage中获取,只读 -->
            <el-input readonly :value="user.username"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select placeholder="请选择车牌号" >
              <el-option label="1" value="10"></el-option>
              <el-option label="2" value="20"></el-option>
              <el-option label="3" value="30"></el-option>
              <el-option label="4" value="40"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select placeholder="请选择" >
              <el-option label="1" value="10"></el-option>
              <el-option label="2" value="20"></el-option>
              <el-option label="3" value="30"></el-option>
              <el-option label="4" value="40"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select placeholder="请选择" >
              <el-option label="1" value="10"></el-option>
              <el-option label="2" value="20"></el-option>
              <el-option label="3" value="30"></el-option>
              <el-option label="4" value="40"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修材料">
            <el-select placeholder="请选择" >
              <el-option label="1" value="10"></el-option>
              <el-option label="2" value="20"></el-option>
              <el-option label="3" value="30"></el-option>
              <el-option label="4" value="40"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用">
            <el-input placeholder="请输入" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="维修状态">
            <el-radio-group>
              <el-radio value="1"  style="margin:0;">维修中</el-radio>
              <el-radio value="0" >已完成</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="addApplication">确定</el-button>
    </template>
  </el-dialog>

  <!-- 用车申请列表 搜索卡片 -->
  <el-card style="margin: 20px;">
    <el-form style="padding-top:10px;">
      <el-row :gutter="30">
        <el-col :span="5">
          <el-form-item label="维修车辆">
            <el-input placeholder="请输入车牌号" v-model="searchApplication.departureAddr"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="送修人员">
            <el-input placeholder="请请输入送修人姓名" v-model="searchApplication.destinationAddr"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="维修状态">
            <el-select placeholder="请选择维修状态" v-model="searchApplication.status">
              <el-option >已完成</el-option>
              <el-option >维修中</el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item>
            <el-button @click="resetSearch">重置</el-button>
            <el-button type="primary" @click="loadApplication">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <!-- 用车申请表格 -->
  <el-card style="margin:20px">
    <el-table :data="tableData">
      <el-table-column type="index" width="80" align="center" prop="id" label="编号"></el-table-column>
      <el-table-column align="center" prop="username" label="维修车辆"></el-table-column>
      <el-table-column align="center" prop="departureAddr" label="维修项目"></el-table-column>
      <el-table-column align="center" prop="destinationAddr" label="送修人员"></el-table-column>
      <el-table-column align="center" prop="reason" label="维修地点"></el-table-column>
      <el-table-column align="center" prop="auditUsernameList" label="维修时间"></el-table-column>
      <el-table-column align="center" prop="startTime" label="维修费用"></el-table-column>
      <el-table-column align="center" prop="endTime" label="维修材料"></el-table-column>
      <el-table-column align="center" prop="status" label="维修状态"
      :formatter="appStatusFormatter"></el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" link
          :disabled="scope.row.status!=10" @click="cancel(scope.row.id)">撤销</el-button>
        </template>
      </el-table-column>
    </el-table>

  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";
//定义变量控制创建申请单弹窗是否出现
const addApplicationDialogVisible = ref(false);

const nisenomo = ref()

//获取当前登录用户
const user = ref(getUser());

//定义数组用来保存审批人选项信息
const auditUserOpts = ref([]);
//定义数组用来保存审批人用户id,新增申请时入库使用
const auditUserIdList = ref([]);
//定义加载审批人的方法
const loadAuditUser = ()=>{
  //获取当前登录用户的父级id
  let parentId = user.value.parentId;
  //查询上级信息
  axios.get(BASE_URL+'/v1/user/select/audit/'+parentId).then((response)=>{
    if (response.data.code==2000){
      //将后端返回的审批人选项存入...Opts数组中
      auditUserOpts.value = response.data.data;
      //遍历...Opts数组,依次将每个审批人,id取出,存入...IdList数组中
      for (let i = 0; i<auditUserOpts.value.length ;i++){
        //...IdList数组保存的多个审批人id值要新增时使用
        auditUserIdList.value.push(auditUserOpts.value[i].id);
      }
    }
  })
}

onMounted(()=>{
  loadAuditUser();
})

/**图片上传相关代码开始**/
const fileList = ref([]);
const dialogImageUrl = ref('');
const dialogVisible = ref(false);
//移除图片
const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
  //1.得到要删除的图片路径 uploadFile就是我们上传的图片文件对象
  let imgUrl = uploadFile.response.data;
  //2.给后端发请求,删除图片
  axios.post(BASE_URL+'/v1/file/remove?imgUrl='+imgUrl).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('删除成功!');
    }
  })

}
//图片上传后预览
const handlePictureCardPreview = (uploadFile) => {
  dialogVisible.value = true;
  console.log(uploadFile);
}
/**图片上传相关代码结束**/

//定义变量用来保存起止时间
const times = ref([]);
//创建对象用来保存申请单的数据
const addForm = ref({
  departureAddr:'',//出发地
  destinationAddr:'',//目的地
  reason:'',//用车事由
  remark:''//备注
});
//新增用车申请
const addApplication = ()=>{
  //确保用户已上传图片
  if (fileList.value.length==0){
    ElMessage.error("请上传图片!");
    return;
  }
  //从upload上传组件中获取上传的图片路径 /2024/05/20/UUID.png ,保存到addForm的imgUrl属性中
  //fileList.value[0] 第1个图片对象
  //.response.data是上传Controller中upload方法JsonResult返回的当前图的/2024/05/20/UUID.png
  addForm.value.imgUrl = fileList.value[0].response.data;//驾照图片路径
  addForm.value.userId = user.value.id;//申请人id
  addForm.value.username = user.value.username;//申请人姓名
  addForm.value.auditUserIdList = auditUserIdList.value;//审批人id集合
  addForm.value.startTime = times.value[0];//开始时间
  addForm.value.endTime = times.value[1];//结束时间
  console.log(addForm.value);
  //将准备好的数据转为查询字符串格式,给后端发新增请求
  let data = qs.stringify(addForm.value);
  axios.post(BASE_URL+'/v1/application/save',data).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('添加完成!');
      //TODO 后面写完查询后,新增完毕也需要重新加载所有的申请单数据!
      loadApplication();
      addApplicationDialogVisible.value = false;
      addForm.value = {};
      fileList.value = [];
      times.value = [];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}

//处理关闭弹窗
const handleClose = ()=>{
  if (confirm("您确认取消申请吗?")){
    //关闭申请弹窗/清空新增表单数据/清空上传图片数据/清空日期组件数据
    addApplicationDialogVisible.value = false;
    addForm.value = {};
    fileList.value = [];
    times.value = [];
  }
}

//准备数组用来存申请单列表表格数据
const tableData = ref([]);
//准备对象保存查询卡片条件
const searchApplication = ref({
  departureAddr:'',
  destinationAddr:'',
  status:''
});
//定义加载申请单的方法
// const loadApplication = ()=>{
//   let data = qs.stringify(searchApplication.value)
//   axios.get(BASE_URL+'/v1/application/select?'+data).then((response)=>{
//     if (response.data.code==2000){
//       tableData.value = response.data.data;
//     }else{
//       ElMessage.error(response.data.msg);
//     }
//   })
// }
//重置搜索卡片
// const resetSearch = ()=>{
//   searchApplication.value = {};
//   loadApplication();
// }
// onMounted(()=>{
//   loadApplication();
// })

const auditOptions = ref([
  {value:'10',label:'已发起'},
  {value:'20',label:'撤销'},
  {value:'30',label:'审核中'},
  {value:'40',label:'驳回'},
  {value:'50',label:'已通过'},
  {value:'60',label:'分配用车'},
  {value:'70',label:'工单结束'}
]);

//状态码格式转换
const appStatusFormatter = (row,column,cellValue,index)=>{
  if (cellValue==10){
    cellValue = '已发起';
  }else if (cellValue==20){
    cellValue = '撤销';
  }else if (cellValue==30){
    cellValue = '审核中';
  }else if (cellValue==40){
    cellValue = '驳回';
  }else if (cellValue==50){
    cellValue = '已通过';
  }else if (cellValue==60){
    cellValue = '分配用车';
  }else if (cellValue==70){
    cellValue = '工单结束';
  }
  return cellValue;
}

//撤销申请
const cancel = (id)=>{
  axios.post(BASE_URL+'/v1/application/cancel/'+id).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('已撤销申请单!');
      loadApplication();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}


</script>

<style scoped>
</style>